<template>
	<view class="login_page">
		<view class="content">
			<view class="logo">
				<image src="../../static/logo.png" mode="widthFix"></image>
			</view>
			<view class="explain">
				<text>前叉之家商户服务小程序</br>登录以使用功能</text>
			</view>
			<view class="btn_wrap">
				<button class="login_btn uni-white uni-success-bg" @click="login">登录</button>
				<button class="resgiter_btn btn uni-success" @click="goToNoLoginPage('/pages/register/register')">注册</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	import { doLogin,judeRegister } from '@/request/api.js'
	import { toast,useRouter } from '@/request/utils.js'
	export default {
		data() {
			return {

			};
		},
		computed: {
			...mapGetters(['userInfo'])
		},
		mounted(){
			console.log(this.userInfo,this.a)
		},
		methods: {
			...mapMutations(['setLogin']),
			login(){
				uni.login({
					success: async (res)=> {
						if (res.code) {
							//发起网络请求
							let data = await doLogin({code:res.code});
							let openid = data.data.openid;
							let unionid = data.data.unionid;
							let registerData = await judeRegister({
								userOpenId: openid,
								userUnionId: unionid
							})
							if(registerData.code==400){
								// 没有注册
								this.goToNoLoginPage('/pages/register/register','navigateTo');
								toast('请先注册')
								this.setLogin(false);
							}else{
								this.goToNoLoginPage('/pages/index/index','switchTab');
								this.setLogin(true);
							}
						}
					},
					fail: (res)=> {
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login_page {
		display: flex;
		flex-direction: column;
		justify-content: center;
		justify-content: center;
		width: 100%;
		height: 100%;
	}

	.content {
		width: 55%;
		margin: auto;
		margin-top: 160rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		.logo {
			width: 50%;
			margin: auto;
			border-radius: 50%;
			overflow: hidden;
		}

		.explain {
			text-align: center;
			margin-top: 40rpx;
			font-size: 29rpx;
		}

		.btn_wrap {
			width: 100%;
			margin-top: 80rpx;
		}

		.btn {
			margin-top: 30rpx;
		}
	}
</style>
